<script setup>
import { ref } from 'vue';
import { Edit, Delete, Operation } from '@element-plus/icons-vue';
import { getRoleListService, deleteRoleService } from '@/apis/system-manage';
import { CommonElePlusTableEventHandler as TableEventHandler } from '@/utilities/tableeventhandle';
import RoleEdit from './components/RoleEdit.vue';
import RoleMenuEdit from './components/RoleMenuEdit.vue';

const loading = ref(false);
const params = ref({
  roleName: ''
});

const roleList = ref([]);
const getRoleList = async () => {
  loading.value = true;
  let res = await getRoleListService(params.value);
  roleList.value = res.data;

  loading.value = false;
};

getRoleList();

const tableEventHanler = new TableEventHandler(params, getRoleList);

const onSearch = tableEventHanler.onSearch;
const onReset = tableEventHanler.onResetFunc(() => {
  params.value.roleName = '';
});

let roleEditRef = ref();
const onEditRole = (id) => {
  roleEditRef.value.open(id);
};

let roleMenuEditRef = ref();
const onEditRoleMenu = (row) => {
  roleMenuEditRef.value.open(row.id);
};

// 删除分类
const confirmDialogVisible = ref(false);
const toDeleteItem = ref({});
const onDeleteRole = (row) => {
  toDeleteItem.value = row;
  confirmDialogVisible.value = true;
};
const doDeleteRole = async () => {
  confirmDialogVisible.value = false;

  loading.value = true;
  let res = await deleteRoleService({ ids: [toDeleteItem.value.id] });
  loading.value = false;
  ElMessage.success(res.msg);
  getRoleList();
};

const onFinishEdit = onReset;
</script>

<template>
  <page-container title="角色管理">
    <template #extra>
      <el-button @click="onEditRole('')">添加角色</el-button>
    </template>

    <!--表单区域-->
    <el-form inline>
      <el-form-item label="角色名称">
        <el-input v-model="params.roleName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="roleList" v-loading="loading">
      <el-table-column prop="roleName" label="角色名称"></el-table-column>
      <el-table-column prop="roleCode" label="角色编码"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            plain
            :icon="Operation"
            @click="onEditRoleMenu(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditRole(row.id)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDeleteRole(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </page-container>

  <role-edit ref="roleEditRef" @finishEdit="onFinishEdit"></role-edit>
  <role-menu-edit ref="roleMenuEditRef"></role-menu-edit>

  <el-dialog v-model="confirmDialogVisible" title="提示" width="30%">
    <span class="dialog-span">
      是否确定删除角色：
      <strong>{{ toDeleteItem && toDeleteItem.roleName }}</strong>
    </span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="confirmDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="doDeleteRole()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped>
.dialog-span {
  font-size: 16px;
}
</style>
